package com.ynny.service;

import com.ynny.bean.Score;
import com.ynny.dao.ScoreManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("scoreManage")
public class ScoreManageImpl implements ScoreManage {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void addScore(Score score) {
        String sql = "insert into score(number,Chinese,math,English) values(?,?,?,?)";
        Object[] obj = new Object[]{
                score.getNumber(),score.getChinese(),score.getMath(),score.getEnglish()

        };

       int num = jdbcTemplate.update(sql, obj);
       if (num <= 0){
           System.out.println("插入学生成绩记录失败");
       }

    }


    @Override
    public void updateScore(String number, String course, Double score) {
        String sql = "update score set "+course+"=? where number=?";
        Object[] params = new Object[]{score,number};
        int num = jdbcTemplate.update(sql, params);
        if (num <= 0){
            System.out.println("更新学生成绩记录失败");
        }

    }

    @Override
    public void queryScoreByNum(String number) {
        String sql = "select * from score where number=?";
        RowMapper<Score> rowMapper;
        rowMapper= new BeanPropertyRowMapper<Score>(Score.class);
        Score score = jdbcTemplate.queryForObject(sql, rowMapper, number);
        System.out.println(score);

    }

    @Override
    public void queryAllScore() {
        String sql = "select * from scores";
        RowMapper<Score> rowMapper;
        rowMapper= new BeanPropertyRowMapper<Score>(Score.class);
        List<Score> scores = jdbcTemplate.query(sql, rowMapper);
        for (Score score : scores){
            System.out.println(score);
        }

    }
}
